<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\builder\Builder;
use app\admin\model\auth\MyAuth;
use app\admin\model\getdatas\MySubject;
use app\admin\model\MyCid;
use app\admin\model\myshuju\MyHandle;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Mycolumn extends Typeone
{
    public function show_list(Request $request)
    {
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $model = $obj->getModel();
        $field = $obj->getField();
//        echo '<pre>';
//        print_r($field);
//        exit;
        $builder = Builder::table()
            ->setAction('mycolumn/page?model_id='.$model_id)
            ->setModelId($model_id)
            ->setMonitorAction('switch','typeone/change')
            ->setMonitorAction('edit','typeone/change')
            ->setRightHeader();
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'mycolumn/del');
            }
            if($value=='add'){
                $builder = $builder->addToolbarDemo('add','添加',[],'mycolumn/show_add');
            }
            if($value=='back'){
                $builder = $builder->addToolbarDemo('back','返回');
            }
            if($value=='search'){
                $builder = $builder->addToolbarDemo('search','搜索');
            }
        }
        foreach ($field as $value){
            if($value['field']=='pid'){
                View::assign('woqu',1);
                $builder = $builder->setColumn('input','pidname',$value['field_name'],'typeone/change');
                continue;
            }
            if($value['field']=='name'){
                $builder = $builder->setColumn('pid',$value['field'],$value['field_name'],'typeone/change');
                continue;
            }
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'typeone/change');
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'typeone/change',null,'center','',$array);
                }
            }
        }
        $builder->setButton('a','字段','editcid/show_list');
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','mycolumn/show_edit');
            }
            if($value=='del'){
                $builder->setButton($value,'删除','mycolumn/del');
            }
            if($value=='add'){
                $builder->setButton($value,'添加','mycolumn/show_add');
            }
            if($value=='a'){
//                $builder->setButton($value,'字段','typeone/show_list');
            }
        }
        return $builder
//            ->setPage()
            ->fetch();
    }
    public function page(Request $request)
    {
//        echo '<pre>';
        $data = MyHandle::GetPage()->getModelPage($request,2,function($value){
            if($value['pid']==0){
                $value['pidname'] = '最上级';
            }else{
                $cid = Db::name(getTable('column'))
                    ->where('id',$value['pid'])
                    ->find();
                if($cid){
                    $value['pidname'] = $cid['name'];
                }
            }
            return $value;
        });
//        exit;
        return json_page_response($data);
    }
    public function show_add(Request $request)
    {
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        if($data['id']??null){
            return $obj->getForm('mycolumn/add',null,true,['show','edit','search','export'],$data['id']);
        }
        return $obj->getForm('mycolumn/add',null,true,['show','edit','search','export']);
    }
    public function add(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(2);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
//        $field = $obj->getField();
        $array = ['增','删','改','查'];
        $newid = Db::name($table)->insertGetId($data);
        foreach ($array as $value){
            Db::name(getTable('jurs'))
                ->insert([
                   'name'=>$data['name'],
                    'descs'=>$value,
                    'status'=>1,
                    'create_time'=>time(),
                    'cid'=>$newid
                ]);
        }
        if($newid){
            MyAuth::add();
            if($data['model_id']??null){
                MyCid::createTable($data['model_id'],$newid);
            }
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function show_edit(Request $request)
    {
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $field = $obj->getField();
        $table  = $obj->getModel('table');
        $data = Db::name($table)
            ->where('id',$data['id'])
            ->find();
        $builder =  Builder::form()
            ->setAction('mycolumn/edit')
            ->setProject('none','id','',$data['id']);
        foreach ($field as $value){
            if($value['form_type']=='单行文本'){
                if($data){
                    $builder = $builder->setProject('input',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                }else{
                    $builder = $builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }
            if($value['form_type']=='时间'){
                if($data){
                    $builder = $builder->setProject('time',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                }else{
                    $builder = $builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                }
            }
            //开关
            if($value['form_type']=='开关'){
                if($data){
                    $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                }else{
                    $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                }
            }
            if($value['form_type']=='联动下拉框'){
                if($value['field']=='model_id'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = Db::name($mytable['table'])->where('status',1)->where('delete_time',0)->where('type','<>',1)->field('id,name')->select()->toArray();
                }else{
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                }
                if($data){
                    $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                }else{
                    $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                }
            }
            //多选框
            if($value['form_type']=='多选框'){
                if($value['field']=='show'||$value['field']=='edit'||$value['field']=='search'||$value['field']=='export'){
                    if($data['model_id']??null){
                        $myfield = Db::name(getTable('fields'))
                            ->where('model_id',$data['model_id'])
                            ->select();
//                        echo '<pre>';
//                        print_r($myfield);exit;
                        $array = [];
                        foreach ($myfield as $v){
                            $array[] = $v['field'];
                        }
                    }else{
                        $array = [];
                    }

                }else{
                    $array = explode(',',$value['option']);
                }
                if($data){
                    $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                }else{
                    $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            //下拉框
            if($value['form_type']=='下拉框'){
                $array = explode(',',$value['option']);
                if($data){
                    $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                }else{
                    $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($data){
                    $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                }else{
                    $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                }
            }

//单文件
            if($value['form_type']=='单文件'){
                if($data){
                    $builder = $builder->setProject('file',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                }else{
                    $builder = $builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            //多文件
            if($value['form_type']=='多文件'){
                if($data){
                    $builder = $builder->setProject('files',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                }else{
                    $builder = $builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='单图片'){
                if($data){
                    $builder = $builder->setProject('img',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                }else{
                    $builder = $builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }
            if($value['form_type']=='多图片'){
                if($data){
                    $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                }else{
                    $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                }
            }

            //单选框
            if($value['form_type']=='单选框'){
                $array = explode(',',$value['option']);
                if($data){
                    $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                }else{
                    $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                }
            }
        }
        $builder = $builder->setButton('submit','提交');
        $builder = $builder->setButton('back','返回')->fetch();
        return $builder;
//        echo '<pre>';
//        print_r($field);
//        exit;
//        return $obj->getForm('mycolumn/edit',$data['id']);
    }
    public function edit(Request $request)
    {
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(2);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        MyCid::updateEdit($data);
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function del(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $table = $obj->getModel('table');
        $newdata = Db::name($table)->where('id','in',$data['id'])->select();
        foreach ($newdata as $value){
            if(isset($value['delete'])&&$value['delete']!==1){
                return json_to_respos('有不可删除数据',205);
            }
        }
        Db::name(getTable('jurs'))
            ->where('cid','in',$data['id'])
            ->delete();
        MyAuth::del();
        if(Db::name($table)->where('id','in',$data['id'])->delete()){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}